package cn.jaye.fabric.controller;

import cn.jaye.fabric.application.AuthApplicationService;
import cn.jaye.fabric.common.constant.Global;
import cn.jaye.fabric.application.command.LoginCommand;
import cn.jaye.fabric.common.constant.ExceptionEnum;
import cn.jaye.fabric.common.exception.ServiceException;
import cn.jaye.fabric.common.model.SystemSession;
import cn.jaye.fabric.common.util.CookieUtil;
import cn.jaye.fabric.common.util.ResultUtil;
import cn.jaye.fabric.domain.model.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Jaye
 * @create 2017-12-20 17:14
 */
@Controller
public class IndexController {

    @Autowired
    private AuthApplicationService authApplicationService;

    @PostMapping("/ping")
    @ResponseBody
    public String ping() {
        return "ok";
    }

    @RequestMapping("/")
    public String index() {
        return "redirect:/index";
    }

    @RequestMapping("/regist")
    public String register() {
        return "regist";
    }

    @RequestMapping("/login")
    public String login() {
        return "login";
    }


    @RequestMapping("/index")
    public String defaultIndex(HttpServletRequest request, Model model) {
        String username = (String) request.getSession().getAttribute("username");
        if (StringUtils.isNotBlank(username)) {
            model.addAttribute("user", "<a class=\"navbar-brand\" href=\"#\">欢迎您" +
                    "，" + username + ",<a class=\"navbar-brand\" id=\"loginout\">注销</a></a>");
        } else {
            model.addAttribute("user", "<a class=\"navbar-brand\" href=\"login\">登 录</a>");
        }
        return "index";
    }
}
